package com.geofeature;

import java.util.ArrayList;
import java.util.List;

import com.geofeature.models.Task;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
/**
 * Tasks list adapter for ListView
 * 
 * @author sheremetat
 *
 */
public class TasksListAdapter extends ArrayAdapter<Task> {
	private List<Task> _tasksList = new ArrayList<Task>();
	private int _itemResourceId = 0;
	
	public TasksListAdapter(Context context, int textViewResourceId,
			List<Task> objects) {
		super(context, textViewResourceId, objects);
		
		_tasksList = objects;
		_itemResourceId = textViewResourceId;
	}

	@Override
	public Context getContext() {
		return super.getContext();
	}

	@Override
	public int getCount() {
		return _tasksList.size();
	}

	@Override
	public Task getItem(int position) {
		return _tasksList.get(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
            LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(_itemResourceId, null);
        }
		
		final Task task = _tasksList.get(position);
		
		// Set task id to view
		TextView taskId = (TextView) view.findViewById(R.id.task_txt_id);
		if(taskId != null){
			taskId.setText("" + task.id + ".");
		}
		
		// Set task title to view
		TextView taskTitle = (TextView) view.findViewById(R.id.task_txt_title);
		if(taskId != null){
			taskTitle.setText(task.name);
		}
		
		// Set Delete button action
		ImageView deleteBtn = (ImageView) view.findViewById(R.id.task_img_delete);
		if(deleteBtn != null){
			deleteBtn.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					Toast.makeText(getContext(), "Delete task: " + task.id + " :: " + task.name , Toast.LENGTH_SHORT).show();
				}
			});
		}
		
		return view;
	}

	
	
}
